var app = getApp()
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    transfer_point: 0,
    loading:false,
    credit1: "",
    point_name: "",
    integral_name: "",
    transfer_point_ratio: 0,
    transfer_integral_ratio: 0,
    practical_num:0,
    //积分转爱心值
    loveByMember:0,
    transfer_integral:0, //积分
    transfer_integral_love:0, //爱心值
    exchange_num:0, //转换的积分
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.loveByMember) {
      this.setData({
        loveByMember: options.loveByMember
      })
    }
    let info_basic = wx.getStorageSync('yz_basic_info');
    this.setData({
      love: info_basic.love,
      point:info_basic.point,
    })
    console.log(this.data.loveByMember)
    this.data.loveByMember?this.getLoveData():this.getData();

  },
  onChange(e){
    this.setData({transfer_point:e.detail})
    if(e.detail>0){
      let res = 0,
        value = 0;
      let practical_num = 0;
      if(this.data.loveByMember) {
        res = this.data.transfer_integral_love/this.data.transfer_integral;
      } else {
        res = this.data.transfer_integral_ratio/this.data.transfer_point_ratio;
      }
      value = Math.floor(res * Math.pow(10, 10)) / Math.pow(10, 10) * e.detail;
      practical_num = Math.floor(value * Math.pow(10, 2)) / Math.pow(10, 2);
      this.setData({practical_num: practical_num})
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getLoveData() {
    let that = this;
    let urlStr = app.getNetAddresss("finance.point-page.index");
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            credit1: res.data.credit1,
            transfer: res.data.transfer,
            transfer_integral:res.data.transfer_integral,
            transfer_integral_love: res.data.transfer_integral_love,
          });
          let title;
          if(this.data.loveByMember) {
            title = `${this.data.point}转化${this.data.love}`;
          }
          this.data.loveByMember?wx.setNavigationBarTitle({title}):wx.setNavigationBarTitle({title});
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: this.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getData() {
    app._postNetWork({
      url:app.getNetAddresss('finance.point-transfer-integral.page-per'),
      success:(res => {
        if(res.data.result){
          let data = res.data.data;
          this.setData({
            credit1:data.credit1,
            point_name:data.point_name,
            integral_name:data.integral_name,
            transfer_point_ratio:data.transfer_point_ratio,
            transfer_integral_ratio:data.transfer_integral_ratio
          })
          let title =data.point_name +'转化'+data.integral_name;
          if(this.data.loveByMember) {
            title = `${data.point_name}转化${this.data.love}`;
          }
          this.data.loveByMember?wx.setNavigationBarTitle({title}):wx.setNavigationBarTitle({title});
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: this.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        }
      })
    })
    
  },
  submit() {
    this.setData({loading:true});
    let params = {};
    console.log(this.data.loveByMember);
    if(this.data.loveByMember) {
      params.transfer_point = parseFloat(this.data.transfer_point);
      params.urlApi ='finance.point-exchange-love.index';
      params.attribute = 'exchange_num';
    } else {
      params.transfer_point = this.data.transfer_point;
      params.urlApi ='finance.point-transfer-integral.index';
      params.attribute = 'transfer_point';
    }
    let attribute = params.attribute;
    let paramsData = {[attribute]: params.transfer_point};
    app._postNetWork({
      url:app.getNetAddresss(params.urlApi),
      data:paramsData,
      success:(res => {
        this.setData({loading:false})
        if(res.data.result){
          app.tips(res.data.msg)
          this.setData({transfer_point:'',practical_num:0});
          this.data.loveByMember?this.getLoveData():this.getData();

          let page=getCurrentPages(); //获取当前页面的页面栈
          let lastPage=page[page.length-2]; //获取上一个页面的页面栈
          lastPage.onLoad(); //调用onload事件
          setTimeout(() => {
            wx.navigateBack({
              data:1
            })
          },1000);
          
        }else{
          app.tips(res.data.msg)
        }
      })
    })
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})